<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="物流公司">
        <el-input size="small" v-model="listQuery.q_companyName"></el-input>
      </el-form-item>
      <el-form-item label="快递类型">
        <el-select
          size="small"
          v-model="listQuery.q_companyType"
          placeholder="请选择"
        >
          <el-option
            v-for="item in companyType"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="this.getList"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh-left" size="small" @click="reset"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button
        size="small"
        type="primary"
        @click="courierCompany(0)"
        v-permission="['admin/systemNotice/create']"
        >添加</el-button
      >
      <el-button
        size="small"
        type="primary"
        @click="courierCompany(1)"
        v-permission="['admin/systemNotice/update']"
        >修改</el-button
      >
      <el-button
        size="small"
        type="primary"
        @click="deleteCourierCompany"
        v-permission="['admin/systemNotice/del']"
        >删除</el-button
      >
    </div>
    <el-table
      ref="singleTable"
      :data="dataList"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      tooltip-effect="dark"
      style="width: 100%"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="index" align="center" width="50">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        property="companyName"
        label="物流公司名称"
      >
      </el-table-column>
      <el-table-column property="companyMark" label="物流公司代码">
      </el-table-column>
      <el-table-column property="seq" label="排序"> </el-table-column>

      <el-table-column label="快递类型">
        <template slot-scope="scope">
          {{ codeBox.TRANSPORT_MODEL[scope.row.companyType] }}
        </template>
      </el-table-column>

      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ codeBox.DISABLE_STATE[scope.row.state] }}
        </template>
      </el-table-column>

      <el-table-column width="160" property="createTime" label="创建时间">
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog :title="dialogTiele" :visible.sync="dialogFormVisible">
      <el-form
        ref="actCourierType"
        :rules="rules"
        :model="actCourierType"
        label-position="right"
        label-width="100px"
      >
        <el-col :span="18">
          <el-form-item label="物流公司" prop="companyName">
            <el-input v-model="actCourierType.companyName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item label="物流代码" prop="companyMark">
            <el-input v-model="actCourierType.companyMark"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="18">
          <el-form-item label="顺序位" prop="seq">
            <el-input-number
              v-model="actCourierType.seq"
              :min="0"
              :max="99"
              label=""
            ></el-input-number>
            <span class="form-word-hint">序号越小越靠前显示</span>
          </el-form-item>
        </el-col>

        <el-col :span="18">
          <el-form-item label="快递类型" prop="companyType">
            <el-radio-group v-model="actCourierType.companyType">
              <el-radio
                v-for="item in companyType"
                :key="item.value"
                :label="item.value"
                >{{ item.name }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="18">
          <el-form-item label="状态" prop="state">
            <el-radio-group v-model="actCourierType.state">
              <el-radio
                v-for="item in state"
                :key="item.value"
                :label="item.value"
                >{{ item.name }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="submitForm('actCourierType')">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchCourierCompanyList,
  deleteCourierCompany,
  createCourierCompany,
  toEditCourierCompany,
  editCourierCompany
} from "@/api/product";
import { getCodeManager, codeToArray } from "@/api/all";
import Pagination from "@/components/Pagination";
import { Message } from "element-ui";
export default {
  name: "couriercompanylist",
  components: { Pagination },
  data() {
    return {
      dataList: [],
      companyType: [],
      state: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        q_companyName: "",
        q_companyType: ""
      },
      currentRow: null,
      dialogFormVisible: false,
      dialogTiele: "",
      currentType: "", // 当前点击类型，0是新增，1是编辑
      statesList: [],
      companyType: [],
      actCourierType: {
        companyName: "",
        companyMark: "",
        seq: "",
        companyType: "",
        state: ""
      },
      rules: {
        companyName: [
          { required: true, message: "请输入物流公司名称", trigger: "blur" },
          { max: 50, message: "物流公司名称不能超过50个字符", trigger: "blur" }
        ],
        companyMark: [
          { required: true, message: "请输入物流公司代码", trigger: "blur" },
          { max: 50, message: "物流公司代码不能超过50个字符", trigger: "blur" }
        ],
        companyType: [
          { required: true, message: "请选择快递类型", trigger: "change" }
        ],
        seq: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        state: [{ required: true, message: "请选择状态", trigger: "change" }]
      }
    };
  },
  created() {
    getCodeManager("TRANSPORT_MODEL, DISABLE_STATE", () => {
      this.companyType = codeToArray("TRANSPORT_MODEL");
      this.state = codeToArray("DISABLE_STATE");
    });
    this.getList();
  },
  methods: {
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val;
    },
    // 获取列表数据
    getList() {
      this.listLoading = true;
      fetchCourierCompanyList(this.listQuery).then(response => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery.q_companyName = "";
      this.listQuery.q_companyType = "";
      this.getList();
    },
    courierCompany(type) {
      // this.$router.push({ path: '/product/couriercompany/couriercompanyadd' })
      if (type == 0) {
        // type为0的时候是新增
        this.currentType = 0;
        this.dialogTiele = "物流公司添加";
        this.$nextTick(() => {
          this.$refs["actCourierType"].clearValidate();
          this.$refs["actCourierType"].resetFields();
        });
        this.dialogFormVisible = true;
      } else {
        // type为1的时候是编辑
        this.dialogTiele = "物流公司编辑";
        if (!this.currentRow) {
          Message.error("请选择操作行。");
          return;
        }
        this.$nextTick(() => {
          this.$refs["actCourierType"].clearValidate();
        });
        this.currentType = 1;
        this.dialogFormVisible = true;
        toEditCourierCompany({ id: this.currentRow.id }).then(res => {
          this.actCourierType = res.data.data.courierCompany;
          this.actCourierType.companyType =
            res.data.data.courierCompany.companyType + "";
          this.actCourierType.state = res.data.data.courierCompany.state + "";
        });
      }
    },
    editCourierCompany() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$router.push({
        path: "/product/couriercompany/couriercompanyedit",
        query: { id: this.currentRow.id }
      });
    },
    deleteCourierCompany() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$confirm("确定删除吗？", "提示", {
        onfirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteCourierCompany({ id: this.currentRow.id }).then(res => {
            if (res.data.success) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.data.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.currentType == 0) {
            createCourierCompany(this.actCourierType).then(res => {
              if (!res.data.success) {
                Message.error(res.data.message);
                return;
              }
              this.dialogFormVisible = false;
              Message.success("操作成功");
              this.getList();
            });
          } else {
            this.actCourierType.id = this.currentRow.id;
            delete this.actCourierType.content;
            editCourierCompany(this.actCourierType).then(res => {
              if (!res.data.success) {
                Message.error(res.data.message);
                return;
              }
              this.dialogFormVisible = false;
              Message.success("操作成功");
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style scoped>
.filter-container .el-col-6 {
  margin-bottom: 15px;
}
.filter-item {
  margin-bottom: 0;
}
/deep/ .filter-container label {
  font-size: 14px;
  font-weight: 500;
  margin-right: 10px;
}
/deep/ [class*="el-col-"] {
  float: none;
}
</style>
